package com.qf;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

/**
 * 当面付
 * https://opendocs.alipay.com/open/194/105072?pathHash=45357796
 */
public class AlipayTest {


    // 每次测试时修改这个订单编号
    public static String orderNum = "202402201330003";

    public static void main(String[] args) throws AlipayApiException {
        unifiedOrder(); // 下单返回支付url
//        queryOrder(); // 查询订单状态

    }

    //https://opendocs.alipay.com/open/f540afd8_alipay.trade.precreate?scene=19&pathHash=d3c84596
    public static void unifiedOrder() throws AlipayApiException {

        String orderSubject = "测试订单";
        String orderDesc = "订单描述信息";
        String totalAmount = "1";
        String notifyUrl = "http//locahost:9216";

        String serverurl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        String appid="9021000128678566";
        String privatekey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCyFq14+keWgROSycJdloSy3G3YR7BSzvIzaegHDwzWIrz+FHQ1Bpt2yC+972Oc3MLvzBupBjjsZVvlJcuElbQ/w9XBD71vhNlecCLHLYOzpJWP2UsoMaC3hZ6OvXC0qEeFTRmS8MiAvP8x4B5by+LjykQPKeemfOJHqSlZzcXCoh2zqOvVDQVzMjNKozM3Tni1iOuaKjs9wSYX2XcCtrqGnDvO58kecub8PgEwHHi2xfKzDVd3Kn9om2Mg+mUJBHPLU4spfcb7NePe+TekSjueZ63dzvubs1qs53p8iC3KaY2KvMpjQrWpOxewsFAWU4wVIPTuN7LnRR93eQfMAF1tAgMBAAECggEAXjr7nRipCmzP44qYKpb7LfzOrnL4TEiuBSBTonCHNUL6R2KhPnF5jzdLc6+YQKbbCrEwLZZaRlM3dZ8y+bu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sVvBblImOBsz5+1Tkb03bQ/5TyfMmCsF55RQ7NsX/87S5AYcKOp4NWGEZfLR3XBFB";
        String publickey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8mmQJWas1jW9zmGfXOkVjTcjwBxXcku23UFoKPtD9cY2DhuDdBxBGFI9P2p0C+iglCuIc/F7wNQAZI91LNQw5hoTeuB7xYzwKt70oke91FQYjutKjPPLYahfxOXtLX0S1HmQRJpYrwkflRMRQieEiYmgK4pmGUNTvGF+XH3YnBWhlAK0EVaeSYRymp9qOgkHTnVoFBbWQr7iTSr8ppcv2DapzNrhAzA6MokW2AOqoJWONZko8L00ZVmNQcLSkpL1zmcUh5l4KJVtl6BV3r2R5kt/r+AMYfit3LH2lHyxZFrZiBlt3rywA/+wZuKZ8Hjt1hZeTYIWQkFqnxBzAMMXwIDAQAB";

        AlipayTradePrecreateRequest req = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo(orderNum);
        model.setSubject(orderSubject); //订单标题
        model.setBody(orderDesc); //订单描述信息
        model.setTotalAmount(totalAmount);  //支付金额
//        model.setTimeExpire(DateUtil.format(payOrder.getExpiredTime(), DatePattern.NORM_DATETIME_FORMAT));  // 订单超时时间
        req.setNotifyUrl(notifyUrl); // 设置异步通知地址
        req.setBizModel(model);

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(appid);
        alipayConfig.setPrivateKey(privatekey);
        alipayConfig.setAlipayPublicKey(publickey);
        alipayConfig.setServerUrl(serverurl);
        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);
        AlipayTradePrecreateResponse response = client.execute(req);
        System.out.println("将这个url转换为二维码图片即可支付："+response.getQrCode());
//        System.out.println(response.getBody());
    }

    public static void queryOrder() throws AlipayApiException {
        String serverurl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        String appid="9021000128678566";
        String privatekey="MIIEvgIBADANBgkqhkiG9w0BA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CtK0oKOhmj6B6Opoz4BWRf9thSjP8G1ymXiv33oiHTd34iRy7rkCiAmzRf3oRXcWEdFl3gNHPhiW0Gjq0TJ1z6qFRt4Sjo9HHUaTQthGnNWWl7GeQE8jDEgdJdWP1aC0K4hivrT0kbtrOU+wOUSphO5Dfmf5XgjSxAoGBAJsmZTwX0qz0XNdibQdtC/OCt4Z4kl5dzGfEQvMLlg22AcDAz5RTPbWxNTINAlArdMFBBwCeAcg2VnEMdo+TXD2NvFV8KkOfibKM4dFO0w51NauOZn8SNFdQeOWCAQuL4B4tTmqDLCkToLP+Ubla+ZOB8CAFhh6ndMF6xKMQWUG1AoGBAJ1rRNu12lMrxSSZEuAxN/+WtZRKLHMB0/bWMXss4EIMKeG+OPTj5NYguQwZETYZSKOL8zZufjTjXEdN3wVSFRWuM69xfccjQtMqK7qj1kPsVvBblImOBsz5+1Tkb03bQ/5TyfMmCsF55RQ7NsX/87S5AYcKOp4NWGEZfLR3XBFB";
        String publickey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8mmQJWas1jW9zmGfXOkVjTcjwBxXcku23UFoKPtD9cY2DhuDdBxBGFI9P2p0C+iglCuIc/F7wNQAZI91LNQw5hoTeuB7xYzwKt70oke91FQYjutKjPPLYahfxOXtLX0S1HmQRJpYrwkflRMRQieEiYmgK4pmGUNTvGF+XH3YnBWhlAK0EVaeSYRymp9qOgkHTnVoFBbWQr7iTSr8ppcv2DapzNrhAzA6MokW2AOqoJWONZko8L00ZVmNQcLSkpL1zmcUh5l4KJVtl6BV3r2R5kt/r+AMYfit3LH2lHyxZFrZiBlt3rywA/+wZuKZ8Hjt1hZeTYIWQkFqnxBzAMMXwIDAQAB";


        AlipayTradeQueryRequest req = new AlipayTradeQueryRequest();

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(orderNum);
        req.setBizModel(model);


        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(appid);
        alipayConfig.setPrivateKey(privatekey);
        alipayConfig.setAlipayPublicKey(publickey);
        alipayConfig.setServerUrl(serverurl);
        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryResponse response = client.execute(req);
        System.out.println(response.getBody());
        System.out.println("订单支付状态为:"+response.getTradeStatus());
    }



}
